#include "address.h"

static void printHelp() {
    printf("-----------------------------------------------------\n");
    printf("\t\t\tAddressList System\t\t\t\n");
    printf("\n");
    printf("\t\t\t1.Search Contact\n");
    printf("\t\t\t2.New Contact\n");
    printf("\t\t\t3.Update Contact\n");
    printf("\t\t\t4.Delete Contact\n");
    printf("\t\t\t5.Exit\n");
    printf("-----------------------------------------------------\n");
}

int Menu(LinkList head) {
    while (1) {
        printHelp();

        List(head);

        char ch = getchar();

        while(getchar() != '\n') {
            continue;
        }

        switch (ch) {
            case '1':
                Search(head);
                break;
            case '2':
                New(head);
                break;
            case '3':
                Update(head);
                break;
        }
    }

    return 0;
}

int Init(LinkList *head) {
    *head = (LinkList) malloc(sizeof(Node));

    strcpy((*head)->name, "\0");
    strcpy((*head)->phone, "\0");

    (*head)->next = NULL;
    return 0;
}

int List(LinkList head) {
    bool has = false;

    printf("list the all contact\n");

    while (head != NULL) {
        if(strcmp(head->name, "\0") != 0) {
            has = true;
            printf("%s's phone number is %s\n", head->name, head->phone);
        }
        head = head->next;
    }

    if (!has) {
        printf("there are no record!\n");
    }

    return 0;
}

int New(LinkList head)
{
    LinkList node = malloc(sizeof(Node));

    if(node == NULL) {
        perror("malloc");
    }

    char name[MAX_NAME_LEN];
    char phone[MAX_PHONE_LEN];

    printf("input name:\n");
    fgets(name, MAX_NAME_LEN, stdin);

    printf("input phone number:\n");
    fgets(phone, MAX_PHONE_LEN, stdin);

    strcpy(node->name, name);
    strcpy(node->phone, phone);

    node->next = head->next;
    head->next = node;

    return 0;
}

int Update(LinkList head)
{
    char name[MAX_NAME_LEN];
    char phone[MAX_PHONE_LEN];

    char input[MAX_PHONE_LEN];

    printf("input name or phone:\n");
    fgets(input, MAX_PHONE_LEN, stdin);

    LinkList old = head;
    while(head != NULL) {
        if(strcmp(head->name, input) == 0 || strcmp(head->phone, input) == 0) {
            printf("searched!\n");
            printf("input new name:\n");
            fgets(name, MAX_NAME_LEN, stdin);

            printf("input new phone number:\n");
            fgets(phone, MAX_PHONE_LEN, stdin);
            strcpy(head->name, name);
            strcpy(head->phone, phone);
            break;
        }
        head = head->next;
    }

    head = old;
    printf("sorry,not found\n");

    return 0;
}

int Search(LinkList head)
{
    char input[MAX_PHONE_LEN];

    printf("input name or phone:\n");
    fgets(input, MAX_PHONE_LEN, stdin);

    printf("this is %s\n", input);

    LinkList list = head;

    while(list != NULL) {
        if(strcmp(list->name, input) == 0 || strcmp(list->phone, input) == 0) {
            printf("searched!name=%s,phone=%s", list->name, list->phone);
            return 0;
        }
        list = list->next;
    }

    printf("sorry,not found\n");
    return 0;
}